package com.extension.company.model.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.extension.company.model.entity.AreaData;

import java.util.ArrayList;
import java.util.List;


/**
 * 本地的数据库的操作
 * 增
 * 删
 * 改
 * 查
 */
public class ReadAreaDataBase {
	private AreaDataBase areaDataBase;

	public ReadAreaDataBase(Context context) {
		areaDataBase = new AreaDataBase(context);
	}

	/**
	 * 查询所有
	 *
	 * @return AreaData
	 */
	public List<AreaData> queryAll() {
		SQLiteDatabase database = areaDataBase.openDatabase();
		List<AreaData> areaDatas = new ArrayList<AreaData>();
		if (database != null) {
			Cursor cursor = database.query("area", null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				AreaData data = new AreaData();
				data.setId(cursor.getString(cursor.getColumnIndex("id")));
				data.setName(cursor.getString(cursor.getColumnIndex("name")));
				data.setParentid(cursor.getString(cursor.getColumnIndex("parentid")));
				areaDatas.add(data);
			}
			cursor.close();
			database.close();
		}
		return areaDatas;
	}

	/**
	 * 根据parentid查询
	 *
	 * @param parentid
	 * @return areaDatas
	 */
	public List<AreaData> query(String parentid) {
		SQLiteDatabase database = areaDataBase.openDatabase();
		List<AreaData> areaDatas = new ArrayList<AreaData>();
		if (database != null) {
			Cursor cursor = database.query("area", null, "parentid=?", new String[]{parentid}, null, null, null);
			while (cursor.moveToNext()) {
				AreaData data = new AreaData();
				data.setId(cursor.getString(cursor.getColumnIndex("id")));
				data.setName(cursor.getString(cursor.getColumnIndex("name")));
				data.setParentid(cursor.getString(cursor.getColumnIndex("parentid")));

				areaDatas.add(data);
			}
			cursor.close();
			database.close();
		}
		return areaDatas;
	}

}
